﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace BookReader
{
    class Program
    {
        static void Main(string[] args)
        {
            InitStaticData();
            Console.WriteLine("A Book Reader,input help or press F1 for command description");
            while (true)
            {
                if (StaticData.Reading)
                {
                    ICommand Command = null;
                    ConsoleKeyInfo k = Console.ReadKey(true);
                    switch (k.Key)
                    {
                        case ConsoleKey.UpArrow:
                            Command = new uCommand();
                            Command.Execute(null);
                            Service.Save();
                            continue;
                        case ConsoleKey.DownArrow:
                            Command = new dCommand();
                            Command.Execute(null);
                            Service.Save();
                            continue;
                        case ConsoleKey.B:
                            Command = new bCommand();
                            Command.Execute(null);
                            Service.Save();
                            continue;
                        case ConsoleKey.F1:
                            Command = new helpCommand();
                            Command.Execute(null);
                            Service.Save();
                            continue;
                        case ConsoleKey.Enter:
                            break;
                        default:
                            Console.WriteLine();
                            continue;
                    }
                }
                string cmdline = Console.ReadLine();
                if (!Execcommand(cmdline))
                {
                    break;
                }
            }
        }

        private static void InitStaticData()
        {
            StaticData.lines = new List<string>();
            StaticData.config = new Config();
            string configPath = AppDomain.CurrentDomain.BaseDirectory + "config.xml";
            if (File.Exists(configPath))
            {
                StaticData.config.ReadXml(configPath, System.Data.XmlReadMode.ReadSchema);
                StaticData.workingDir = StaticData.config.ConfigData[0].workingDir;
                StaticData.pageSize = StaticData.config.ConfigData[0].pageSize;
                StaticData.currentIndex = StaticData.config.ConfigData[0].currentIndex;
                StaticData.openFilepath = StaticData.config.ConfigData[0].openFilepath;
                StaticData.PageIndex = StaticData.config.ConfigData[0].pageIndex;
                StaticData.encoding = Encoding.GetEncoding(StaticData.config.ConfigData[0].CodePage);
                ICommand cmd = new opCommand();
                cmd.Execute(new string[] { StaticData.openFilepath });
            }
            else
            {
                StaticData.workingDir = AppDomain.CurrentDomain.BaseDirectory;
                StaticData.pageSize = 20;
                StaticData.currentIndex = 0;
                StaticData.openFilepath = string.Empty;
                StaticData.encoding = Encoding.Default;
                Config.ConfigDataRow configitem = StaticData.config.ConfigData.NewConfigDataRow();
                configitem.workingDir = StaticData.workingDir;
                configitem.pageSize = StaticData.pageSize;
                configitem.currentIndex = StaticData.currentIndex;
                configitem.openFilepath = string.Empty;
                configitem.CodePage = StaticData.encoding.CodePage;
                configitem.pageIndex = StaticData.PageIndex;
                configitem.key = Guid.NewGuid().ToString();
                StaticData.config.ConfigData.AddConfigDataRow(configitem);
                StaticData.config.WriteXml(configPath, System.Data.XmlWriteMode.WriteSchema);
            }
        }
        static bool Execcommand(string cmd)
        {
            string param = string.Empty;
            string command = string.Empty;
            string[] cmds = cmd.Split(' ');
            string[] pls = null;
            if (cmds.Length > 1)
            {
                pls = cmds[1].Split(',');
            }
            command = cmds[0];
            return execute(command, pls);
            
        }
        static bool execute(string cmd, string[] param)
        {
            ICommand command = null;
            cmd = cmd.ToLower();
            if (cmd == "exit")
                return false;
            Type tp = Type.GetType("BookReader."+cmd + "Command");
            if (tp == null)
            {
                Console.WriteLine("没有这个指令");
            }
            else
            {
                command = (ICommand)Activator.CreateInstance(tp);
            }
            if (command != null)
            {
                command.Execute(param);
                Service.Save();
            }
            return true;
        }
    }
}
